package bma.common.langutil.ai.stack;

/**
 * 
 * @author guanzhong
 * @since 1.0 (imported from BMA.ESP Project)
 * 
 * @param <CUR_TYPE>
 * @param <NEXT_TYPE>
 */
public abstract class AIStackStep<CUR_TYPE, NEXT_TYPE> extends
		AIStackAbstract<CUR_TYPE> {

	private AIStack<NEXT_TYPE> delegate;

	public AIStackStep(AIStack<NEXT_TYPE> delegate) {
		super();
		this.delegate = delegate;
	}

	public AIStack<NEXT_TYPE> delegate() {
		return delegate;
	}

	@Override
	public AIStack<?> getParent() {
		return delegate;
	}

	@Override
	public boolean success(CUR_TYPE result) {
		try {
			return next(result);
		} catch (Exception e) {
			return this.delegate.failure(e);
		}
	}

	@Override
	public boolean failure(Throwable t) {
		return this.delegate.failure(t);
	}

	public boolean successForward(NEXT_TYPE v) {
		return this.delegate.success(v);
	}

	protected abstract boolean next(CUR_TYPE result);
}
